package com.taoyibao.hongcanting.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.taoyibao.hongcanting.R;


/**
 * 顶部带进度条的WebView
 */

public class ProgressWebView extends RelativeLayout {

    private ProgressBar wbProgressBar;
    private WebView dealWebView;

    public ProgressWebView(Context context) {
        this(context, null);
    }

    public ProgressWebView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.progress_layout, null);
        wbProgressBar = (ProgressBar) view.findViewById(R.id.wbProgressBar);
        dealWebView = (WebView) view.findViewById(R.id.deal_delegate_webView);

        wbProgressBar.setVisibility(View.VISIBLE);

        dealWebView.setWebChromeClient(new WebChromeViewClient());
        this.addView(view);
    }

    public WebView getWebView() {
        return dealWebView;
    }

    private class WebChromeViewClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            wbProgressBar.setProgress(newProgress);
            if (newProgress == 100) {
                wbProgressBar.setVisibility(View.GONE);
            }
        }

    }
}
